# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import paramiko

from django.shortcuts import render
from django.http import HttpResponse
# from django.db import models
from apps.models import Host, h_username, h_password
import json


# Create your views here.


def terminal(request):
    if request.method == 'POST':
        req = request.POST.dict()

        host_id = req['id']

        obj = Host.objects.filter(id=host_id).first()

    ret = {}
    try:
        # if checker.has_perm('task_asset', a) == True:
        if True:
            ip = obj.ipaddress
            print(ip)
            port = 22
            username = obj.user_id.username
            password = obj.password_id.password

            ret = {"ip": ip, 'port': port, "username": username, 'password': password, "static": True}
            login_ip = request.META['REMOTE_ADDR']

            # web_history.objects.create(user=request.user, ip=login_ip, login_user=obj.system_user.username, host=ip)
    except Exception as e:
        ret['status'] = False
        ret['error'] = '请求错误,{}'.format(e)
    finally:
        return HttpResponse(json.dumps(ret))
        return render(request, 'asset/terminal.html', json.dumps(ret))
        # return HttpResponse('11111111---msg.....')


def asset(request):
    settings = {'Webssh_ip': '192.168.1.1', }
    ret = {
        "ip": '192.168.1.1',
        'port': 22,
        "username": 'username',
        'password': 'password',
        "static": True,
        'Webssh_ip': '192.168.1.1',
    }
    return render(request, 'asset/1.html',
                  {"Webssh": 'localhost',
                   "Webssh_port": 22,
                   "asset_active": "active",
                   "asset_list_active": "active", "asset_list": ret})


def host(request):
    """Show Host List Page"""
    if request.method == 'GET':
        host_obj = Host.objects.all()
        # print(type(Host), Host)

        return render(request, 'asset/host.html', {'result': host_obj})
    elif request.method == 'POST':
        return HttpResponse('')
